package org.kl.bf.web.basic;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.Calendar;
import org.kl.bf.entity.basic.CalendarType;
import org.kl.bf.entity.basic.User;
import org.kl.bf.entity.easyui.EasyuiGrid;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.basic.CalendarService;
import org.kl.bf.service.basic.CalendarTypeService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.MediaTypes;

/**
 * 系统配置
 * 
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:34:36
 */
@Controller
@RequestMapping(value = "/basic/calendarManage")
public class CalenderManageController extends BasicController {
	@Autowired
	private CalendarService calendarService;
	@Autowired
	private CalendarTypeService calendarTypeService;

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		if (StringUtils.equals(BasicController.getCurrentUserName(), BasicConstants.Administrator_TrueName)) {
			model.addAttribute("isSystem", 1);
		} else {
			model.addAttribute("isSystem", 0);
		}
		return "basic/calendarManageList";
	}

	@RequestMapping(value = "dataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType,
			@RequestParam(value = "isAll", defaultValue = "false") boolean isAll,
			@RequestParam(value = "isSys", defaultValue = "false") boolean isSys, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		if (searchParams.isEmpty() && !isAll && !isSys) {
			return EasyuiUtils.buildEasyuiGridJsonString(null, 0);
		}
		if (!isSys) {
			searchParams.put("EQ_user.id", BasicController.getCurrentUserId());
		}
		if (isAll) {
			searchParams.remove("EQ_calendarType.id");
		}
		Page<Calendar> calendarType = calendarService.getUserCalendar(searchParams, pageNumber, pageSize, sortType, orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(calendarType.getContent());
		grid.setTotal((int) calendarType.getTotalElements());
		String result = jsonMapper.toJson(grid);
		return result;
	}

	@RequestMapping(value = "newUpdate", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String newUpdate(@ModelAttribute("calendar") Calendar calendar, Model model, RedirectAttributes redirectAttributes,
			HttpSession session) {
		if (calendar.getUser() == null || StringUtils.isBlank(calendar.getUser().getId())) {
			calendar.setUser(new User(BasicController.getCurrentUserId()));
		}
		if (calendar.getCalendarType() != null && StringUtils.isNotBlank(calendar.getCalendarType().getId())) {
			CalendarType calendarType = calendarTypeService.getCalendarType(calendar.getCalendarType().getId());
			calendar.setBackgroundColor(calendarType.getBackgroundColor());
		} else {
			return ajaxMsg("编辑失败,日程类型未选择", false, null);
		}
		try {
			calendarService.saveCalendar(calendar);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("编辑失败[" + e.getMessage() + "]", false, null);
		}
		return ajaxMsg("编辑成功", true, null);
	}

	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		String msg = "";
		try {
			msg = calendarService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功:" + msg, true, null);
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getCalendar(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("calendar", calendarService.getCalendar(id));
		}
	}
}
